package org.zoomdev.android.rn.bugly;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;
import com.tencent.bugly.crashreport.CrashReport;

public class BuglyModule extends ReactContextBaseJavaModule {


    private String appId;

    private boolean debug;

    public BuglyModule(ReactApplicationContext reactContext
            ,String appId,boolean debug) {
        super(reactContext);
        this.appId = appId;
        this.debug = debug;
    }

    @Override
    public String getName() {
        return "BuglyModule";
    }

    @Override
    public void initialize() {
        super.initialize();
        Bugly.init(getReactApplicationContext(), appId, debug);
        CrashReport.initCrashReport(getReactApplicationContext(),
                appId, debug);

    }


    @ReactMethod
    public void checkUpdate(final Promise promise){
        UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
        if (upgradeInfo == null) {
            promise.reject("-1", "No upgrade");
        } else {
            Beta.checkUpgrade();
            promise.resolve((Object)null);
        }
    }
}
